package com.itheima.reggie.controller;

import com.itheima.reggie.entity.*;
import com.itheima.reggie.entity.dto.DishDto;
import com.itheima.reggie.entity.dto.SetmealDishDto;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {

    @Autowired(required = false)
    private SetmealService setmealService;

    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session){
        Long empId = (Long) session.getAttribute("employee");

        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);

        setmealService.save(setmealDto);

        return R.success("保存套餐成功");
    }

    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
                               @ApiImplicitParam(name = "page",value = "页码",required = true),
                               @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
                               @ApiImplicitParam(name = "name",value = "套餐名称",required = true)
    })
    public R<Page<SetmealDto>> findByPage(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "1") Integer pageSize,String name){
        R<Page<SetmealDto>> result = setmealService.findByPage(page,pageSize,name);
        return result;
    }

    @DeleteMapping
    @ApiOperation("删除套餐接口")
    public R<String> delete(@RequestParam Long[] ids){

        setmealService.delete(ids);
        return R.success("删除成功");
    }

    @GetMapping("/{setmealId}")
    @ApiOperation("套餐条件查询接口")
    public R<SetmealDto> findBySetmealId(@PathVariable Long setmealId){
        R<SetmealDto> result = setmealService.findBySetmealId(setmealId);
        return result;
    }

    @PutMapping
    @ApiOperation("更新套餐接口")
    public R<String> update(@RequestBody SetmealDto setmealDto,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealService.update(setmealDto);

        return R.success("保存成功");
    }

    @PostMapping("/status/{status}")
    @ApiOperation("更新状态接口")
    public R<String> updateStatus(@RequestParam List<Long> ids,@PathVariable int status){
        setmealService.updateStatus(ids,status);

        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> findByCategoryId(Long categoryId,int status){
        List<Setmeal> result = setmealService.findByCategoryId(categoryId,status);
        return R.success(result);
    }

    @GetMapping("/dish/{id}")
    public R<List<Dish>> getDish(@PathVariable Long id){
        R<List<Dish>> result = setmealService.getBySetmealId(id);
        return result;
    }


}
